# -*- coding: utf-8 -*-

###########################################################################
## Python code generated with wxFormBuilder (version Aug 24 2018)
## http://www.wxformbuilder.org/
##
## PLEASE DO "NOT" EDIT THIS FILE!
###########################################################################

import wx
import wx.xrc
import logging
from src.bridge.getconfdata import ConfData

logging.basicConfig(level=logging.INFO)


###########################################################################
## Class SetConfPanel
###########################################################################

class SetConfPanel(wx.Panel):

    def __init__(self, parent):
        wx.Panel.__init__(self, parent, id=wx.ID_ANY, pos=wx.DefaultPosition, size=wx.Size(752, 451),
                          style=wx.TAB_TRAVERSAL)
        self.cd = ConfData()

        sbSizer = wx.BoxSizer(wx.HORIZONTAL)

        sgbSizer = wx.GridBagSizer(0, 0)
        sgbSizer.SetFlexibleDirection(wx.BOTH)
        sgbSizer.SetNonFlexibleGrowMode(wx.FLEX_GROWMODE_SPECIFIED)

        self.sdata_staticText = wx.StaticText(self, wx.ID_ANY, u"数据来源：", wx.DefaultPosition, wx.DefaultSize, 0)
        self.sdata_staticText.Wrap(-1)
        sgbSizer.Add(self.sdata_staticText, wx.GBPosition(0, 0), wx.GBSpan(1, 1),
                     wx.ALL | wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_RIGHT, 5)

        sdata_choiceChoices = [u"数据库", u"爬虫"]
        self.sdata_choice = wx.Choice(self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, sdata_choiceChoices, 0)
        self.sdata_choice.SetSelection(
            int(self.cd.get_one_value(section='base', key='datacome')) if self.cd.get_one_value(section='base',
                                                                                                key='datacome') else '')
        sgbSizer.Add(self.sdata_choice, wx.GBPosition(0, 1), wx.GBSpan(1, 1), wx.ALL, 5)

        self.sdata_checkBox = wx.CheckBox(self, wx.ID_ANY, u"浏览器无头模式", wx.DefaultPosition, wx.DefaultSize, 0)
        defcheck = True if self.cd.get_one_value(section='base', key='opt') == '1' else False
        self.sdata_checkBox.SetValue(defcheck)
        sgbSizer.Add(self.sdata_checkBox, wx.GBPosition(0, 2), wx.GBSpan(1, 1), wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5)

        self.sd_staticline = wx.StaticLine(self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.LI_HORIZONTAL)
        sgbSizer.Add(self.sd_staticline, wx.GBPosition(1, 0), wx.GBSpan(1, 4), wx.EXPAND | wx.ALL, 5)

        self.szt_staticText = wx.StaticText(self, wx.ID_ANY, u"禅道地址：", wx.DefaultPosition, wx.DefaultSize, 0)
        self.szt_staticText.Wrap(-1)
        sgbSizer.Add(self.szt_staticText, wx.GBPosition(2, 0), wx.GBSpan(1, 1),
                     wx.ALL | wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_RIGHT, 5)

        zt_url = self.cd.get_one_value(section='ztinfo', key='url') if self.cd.get_one_value(section='ztinfo',
                                                                                             key='url') else ''
        self.szt_textCtrl = wx.TextCtrl(self, wx.ID_ANY, zt_url, wx.DefaultPosition, wx.DefaultSize, 0)
        sgbSizer.Add(self.szt_textCtrl, wx.GBPosition(2, 1), wx.GBSpan(1, 3),
                     wx.ALL | wx.EXPAND | wx.ALIGN_CENTER_VERTICAL, 5)

        self.ztuser_staticText = wx.StaticText(self, wx.ID_ANY, u"用户名：", wx.DefaultPosition, wx.DefaultSize, 0)
        self.ztuser_staticText.Wrap(-1)
        sgbSizer.Add(self.ztuser_staticText, wx.GBPosition(3, 0), wx.GBSpan(1, 1),
                     wx.ALL | wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_RIGHT, 5)

        zt_user = self.cd.get_one_value(section='ztinfo', key='user') if self.cd.get_one_value(section='ztinfo',
                                                                                               key='user') else ''
        self.ztuser_textCtrl = wx.TextCtrl(self, wx.ID_ANY, zt_user, wx.DefaultPosition, wx.DefaultSize, 0)
        sgbSizer.Add(self.ztuser_textCtrl, wx.GBPosition(3, 1), wx.GBSpan(1, 1), wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5)

        self.ztpwd_staticText = wx.StaticText(self, wx.ID_ANY, u"密码：", wx.DefaultPosition, wx.DefaultSize, 0)
        self.ztpwd_staticText.Wrap(-1)
        sgbSizer.Add(self.ztpwd_staticText, wx.GBPosition(3, 2), wx.GBSpan(1, 1),
                     wx.ALL | wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_CENTER_HORIZONTAL, 5)

        zt_pwd = self.cd.get_one_value(section='ztinfo', key='pwd') if self.cd.get_one_value(section='ztinfo',
                                                                                             key='pwd') else ''
        self.ztpwd_textCtrl = wx.TextCtrl(self, wx.ID_ANY, zt_pwd, wx.DefaultPosition, wx.Size(150, -1), 0)
        sgbSizer.Add(self.ztpwd_textCtrl, wx.GBPosition(3, 3), wx.GBSpan(1, 1), wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5)

        self.ztpd_staticText = wx.StaticText(self, wx.ID_ANY, u"产品名称：", wx.DefaultPosition, wx.DefaultSize, 0)
        self.ztpd_staticText.Wrap(-1)
        sgbSizer.Add(self.ztpd_staticText, wx.GBPosition(4, 0), wx.GBSpan(1, 1),
                     wx.ALL | wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL, 5)

        zt_prd = self.cd.get_one_value(section='ztinfo', key='prdouct_cn') if self.cd.get_one_value(section='ztinfo',
                                                                                                    key='prdouct_cn') else ''
        self.ztpd_textCtrl = wx.TextCtrl(self, wx.ID_ANY, zt_prd, wx.DefaultPosition, wx.Size(150, -1), 0)
        sgbSizer.Add(self.ztpd_textCtrl, wx.GBPosition(4, 1), wx.GBSpan(1, 1), wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5)

        self.ztpj_staticText = wx.StaticText(self, wx.ID_ANY, u"项目名称：", wx.DefaultPosition, wx.DefaultSize, 0)
        self.ztpj_staticText.Wrap(-1)
        sgbSizer.Add(self.ztpj_staticText, wx.GBPosition(4, 2), wx.GBSpan(1, 1),
                     wx.ALL | wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_CENTER_HORIZONTAL, 5)

        zt_pj = self.cd.get_one_value(section='ztinfo', key='project_cn') if self.cd.get_one_value(section='ztinfo',
                                                                                                   key='project_cn') else ''
        self.ztpj_textCtrl = wx.TextCtrl(self, wx.ID_ANY, zt_pj, wx.DefaultPosition, wx.Size(150, -1), 0)
        sgbSizer.Add(self.ztpj_textCtrl, wx.GBPosition(4, 3), wx.GBSpan(1, 1), wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5)

        self.ztplan_staticText = wx.StaticText(self, wx.ID_ANY, '产品计划:', wx.DefaultPosition, wx.DefaultSize, 0)
        self.ztplan_staticText.Wrap(-1)
        sgbSizer.Add(self.ztplan_staticText, wx.GBPosition(5, 0), wx.GBSpan(1, 1),
                     wx.ALL | wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_CENTER_HORIZONTAL, 5)

        zt_plan = self.cd.get_one_value(section='ztinfo', key='product_plan') if self.cd.get_one_value(section='ztinfo',
                                                                                                       key='product_plan') else ''
        self.ztplan_textCtrl = wx.TextCtrl(self, wx.ID_ANY, zt_plan, wx.DefaultPosition, wx.Size(150, -1), 0)
        sgbSizer.Add(self.ztplan_textCtrl, wx.GBPosition(5, 1), wx.GBSpan(1, 1), wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5)

        self.ztpjversion_staticText = wx.StaticText(self, wx.ID_ANY, '任务版本:', wx.DefaultPosition, wx.DefaultSize, 0)
        self.ztpjversion_staticText.Wrap(-1)
        sgbSizer.Add(self.ztpjversion_staticText, wx.GBPosition(5, 2), wx.GBSpan(1, 1),
                     wx.ALL | wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_CENTER_HORIZONTAL, 5)

        ztpj_version = self.cd.get_one_value(section='ztinfo', key='project_version') if self.cd.get_one_value(
            section='ztinfo', key='project_version') else ''
        self.ztpjversion_textCtrl = wx.TextCtrl(self, wx.ID_ANY, ztpj_version, wx.DefaultPosition,
                                                wx.Size(150, -1), 0)
        sgbSizer.Add(self.ztpjversion_textCtrl, wx.GBPosition(5, 3), wx.GBSpan(1, 1), wx.ALL | wx.ALIGN_CENTER_VERTICAL,
                     5)

        self.m_staticline1 = wx.StaticLine(self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.LI_HORIZONTAL)
        sgbSizer.Add(self.m_staticline1, wx.GBPosition(6, 0), wx.GBSpan(1, 4), wx.EXPAND | wx.ALL, 5)

        self.dbip_staticText = wx.StaticText(self, wx.ID_ANY, u"数据库ip：", wx.DefaultPosition, wx.DefaultSize, 0)
        self.dbip_staticText.Wrap(-1)
        sgbSizer.Add(self.dbip_staticText, wx.GBPosition(7, 0), wx.GBSpan(1, 1), wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5)

        db_ip = self.cd.get_one_value(section='db', key='local') if self.cd.get_one_value(section='db',
                                                                                          key='local') else ''
        self.dbip_textCtrl = wx.TextCtrl(self, wx.ID_ANY, db_ip, wx.DefaultPosition, wx.Size(150, -1), 0)
        self.dbip_textCtrl.Enable(False)

        sgbSizer.Add(self.dbip_textCtrl, wx.GBPosition(7, 1), wx.GBSpan(1, 1), wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5)

        self.dbport_staticText = wx.StaticText(self, wx.ID_ANY, u"端口：", wx.DefaultPosition, wx.DefaultSize, 0)
        self.dbport_staticText.Wrap(-1)
        sgbSizer.Add(self.dbport_staticText, wx.GBPosition(7, 2), wx.GBSpan(1, 1),
                     wx.ALL | wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_CENTER_HORIZONTAL, 5)

        db_port = self.cd.get_one_value(section='db', key='port') if self.cd.get_one_value(section='db',
                                                                                           key='port') else ''
        self.dbport_textCtrl = wx.TextCtrl(self, wx.ID_ANY, db_port, wx.DefaultPosition, wx.DefaultSize, 0)
        self.dbport_textCtrl.Enable(False)

        sgbSizer.Add(self.dbport_textCtrl, wx.GBPosition(7, 3), wx.GBSpan(1, 1), wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5)

        self.dbuser_staticText = wx.StaticText(self, wx.ID_ANY, u"账户：", wx.DefaultPosition, wx.DefaultSize, 0)
        self.dbuser_staticText.Wrap(-1)
        sgbSizer.Add(self.dbuser_staticText, wx.GBPosition(8, 0), wx.GBSpan(1, 1),
                     wx.ALL | wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_RIGHT, 5)

        db_user = self.cd.get_one_value(section='db', key='user') if self.cd.get_one_value(section='db',
                                                                                           key='user') else ''
        self.dbuser_textCtrl = wx.TextCtrl(self, wx.ID_ANY, db_user, wx.DefaultPosition, wx.DefaultSize, 0)
        self.dbuser_textCtrl.Enable(False)

        sgbSizer.Add(self.dbuser_textCtrl, wx.GBPosition(8, 1), wx.GBSpan(1, 1), wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5)

        self.dbpwd_staticText = wx.StaticText(self, wx.ID_ANY, u"密码：", wx.DefaultPosition, wx.DefaultSize, 0)
        self.dbpwd_staticText.Wrap(-1)
        sgbSizer.Add(self.dbpwd_staticText, wx.GBPosition(8, 2), wx.GBSpan(1, 1),
                     wx.ALL | wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_CENTER_HORIZONTAL, 5)

        db_pwd = self.cd.get_one_value(section='db', key='passwd') if self.cd.get_one_value(section='db',
                                                                                            key='passwd') else ''
        self.dbpwd_textCtrl = wx.TextCtrl(self, wx.ID_ANY, db_pwd, wx.DefaultPosition, wx.Size(150, -1),
                                          wx.TE_PASSWORD)
        self.dbpwd_textCtrl.Enable(False)

        sgbSizer.Add(self.dbpwd_textCtrl, wx.GBPosition(8, 3), wx.GBSpan(1, 1), wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5)

        sbSizer.Add(sgbSizer, 0, 0, 5)

        self.save_button = wx.Button(self, wx.ID_ANY, u"保 存", wx.DefaultPosition, wx.DefaultSize, 0)

        self.save_button.SetDefault()
        sbSizer.Add(self.save_button, 0, wx.ALIGN_CENTER_VERTICAL | wx.ALL, 5)

        conftext = self.cd.open_confile()
        self.conf_textCtrl = wx.TextCtrl(self, wx.ID_ANY, conftext, wx.DefaultPosition, wx.DefaultSize,
                                         wx.TE_MULTILINE)
        self.conf_textCtrl.Enable(False)

        sbSizer.Add(self.conf_textCtrl, 1, wx.ALL | wx.EXPAND, 5)

        self.SetSizer(sbSizer)
        self.Layout()

        # Connect Events
        self.save_button.Bind(wx.EVT_BUTTON, self.save_conf)

    def __del__(self):
        pass

    # Virtual event handlers, overide them in your derived class
    def save_conf(self, event):
        try:
            event.GetEventObject().Disable()
            base = {}
            datachoice = {"数据库": "0", "爬虫": "1"}
            base['datacome'] = datachoice[self.sdata_choice.GetStringSelection()]
            base['opt'] = 1 if self.sdata_checkBox.GetValue() else 0
            ztinfo = {}
            ztinfo['url'] = self.szt_textCtrl.GetValue().strip()
            ztinfo['user'] = self.ztuser_textCtrl.GetValue().strip()
            ztinfo['pwd'] = self.ztpwd_textCtrl.GetValue().strip()
            ztinfo['prdouct_cn'] = self.ztpd_textCtrl.GetValue().strip()
            ztinfo['project_cn'] = self.ztpj_textCtrl.GetValue().strip()
            ztinfo['product_plan'] = self.ztplan_textCtrl.GetValue().strip()
            ztinfo['project_version'] = self.ztpjversion_textCtrl.GetValue().strip()
            db = {}
            db['local'] = self.dbip_textCtrl.GetValue().strip()
            db['user'] = self.dbuser_textCtrl.GetValue().strip()
            db['passwd'] = self.dbpwd_textCtrl.GetValue().strip()
            db['port'] = self.dbport_textCtrl.GetValue().strip()
            db['dbname'] = 'zentao'
            with open(self.cd.confile, 'w+', encoding='utf-8-sig') as f:
                for d in ['base', 'ztinfo', 'db']:
                    f.writelines('[{}]'.format(d) + '\n')
                    for k, v in eval(d).items():
                        f.writelines('{} = {}'.format(k, v) + '\n')
                    f.writelines('\n' * 2)
            self.conf_textCtrl.Clear()
            self.conf_textCtrl.SetValue(self.cd.open_confile())
        except BaseException as e:
            logging.error(e)
        finally:
            event.GetEventObject().Enable()
            event.Skip()
